<template>
  <div class="home-container">
    <!-- 顶部导航栏组件 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button class="search-btn" slot="title" type="info" size="small" round icon="search" to="/search">搜索</van-button>
    </van-nav-bar>
    <van-tabs v-model="active" class="channel-tabs" swipeable animated>
      <van-tab :title="channel.name" v-for="channel in channels" :key="channel.id">

        <!-- 文章列表 -->
        <articleListVue ref="article-list" :channel="channel" />
        <!-- 文章列表 -->
      </van-tab>
      <div slot="nav-right" class="placeholder"></div>
      <div slot="nav-right" class="hamburger-btn" @click="isChannelEditShow = true">
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <van-popup v-model="isChannelEditShow" closeable close-icon-position="top-left" position="bottom"
      :style="{ height: '100%' }">
      <channelEditVue :my-channels="channels" :active-index="active" @update-artive="onUpdataActive" />
    </van-popup>
  </div>
</template>
  
<script>
import { getUserChannels } from "@/api/user"
// import { channel } from "diagnostics_channel";
import articleListVue from "./components/article-list.vue"
import { mapState } from 'vuex'
import channelEditVue from "./components/channel-edit.vue"
import {getItem} from '@/utils/storeage'
export default {
  name: "HomeIndex",
  data() {
    return {
      active: 0,
      channels: [],
      isChannelEditShow: false
    };
  },
  created() {
    this.loadChannels();
  },
  computed: {
    ...mapState(['user'])
  },
  methods: {
    async loadChannels() {
      try {
        let channels = []
        if (this.user) {
          const { data } = await getUserChannels();
          // console.log(data);
         channels = data.data.channels;
        }else{
          const locaChannels = getItem('TOUTIAO_CHANNELS')
          if(locaChannels){
            channels=locaChannels
          }else{
            const {data} = await getUserChannels()
            channels=data.data.channels
          }
        }
        this.channels = channels
      } catch (err) {
        this.$toast("获取频道数据失败");
      }
    },
    onUpdataActive(index, isChannelEditShow = true) {
      this.active = index
      this.isChannelEditShow = isChannelEditShow
    }

  },
  components: {
    articleListVue, channelEditVue
  }
};
</script>
  
<style scoped lang="less">
.home-container {
  padding-top: 174px;

  /deep/ .van-nav-bar__title {
    max-width: unset;
  }

  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;

    .van-icon {
      font-size: 32px;
    }
  }

  /deep/ .channel-tabs {
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 1;
      left: 0;
      right: 0;
      height: 82px;
    }

    .van-tab {
      min-width: 200px;
      border-right: 1px solid #edeff3;
      font-size: 30px;
      color: #777777;
    }

    .van-tab--active {
      color: #333333;
    }

    .van-tabs__nav {
      padding-bottom: 0;
    }

    .van-tabs__line {
      bottom: 8px;
      width: 31px !important;
      height: 6px;
      background-color: #3296fa;
    }
  }

  .hamburger-btn {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 82px;
    background-color: #fff;
    opacity: 0.902;

    i.toutiao {
      font-size: 33px;
    }
  }

  .placeholder {
    flex-shrink: 0;
    width: 66px;
    height: 82px;
  }

  .hamburger-btn {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 82px;
    background-color: #fff;
    opacity: 0.902;

    i.toutiao {
      font-size: 33px;
    }

    &:before {
      content: "";
      position: absolute;
      left: 0;
      width: 1px;
      height: 100%;
      background-image: url(~@/assets/gradient-gray-line.png);
      background-size: contain;
    }
  }
}
</style>